package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@RestController
public class OrderController {

    private static final String url = "http://product-service/product";
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/")
    public String home() {
        return "Hello World!";
    }

    @GetMapping("/service")
    public List service() {
        return serviceUrl();
    }

    @GetMapping("/product")
    public String getProduct() {
        // 服务之间调用
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println(forObject);
        return forObject;
    }


    public List<ServiceInstance> serviceUrl() {
        return discoveryClient.getInstances("application");
    }
}
